package cn.stylefeng.guns.core.util;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * httpclient工具类
 *
 * @author zxw 2022/12/19 8:35
 */
public class HttpClientUtil {

    private static final CloseableHttpClient client = HttpClientBuilder.create().build();

    private static final String DEFAULT_CHARSET = "UTF-8";

    /**
     * get方式调用httpclient服务
     *
     * @param url     请求地址
     * @param param   请求参数
     * @param headers 请求头信息
     * @return
     */
    public static String doGet(String url, Map<String, String> param, Map<String, String> headers) {

        String resultString = "";
        CloseableHttpResponse response = null;

        try {
            URIBuilder uri = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    uri.addParameter(key, param.get(key));
                }
            }

            HttpGet get = new HttpGet(uri.build());
            if (headers != null) {
                for (String key : headers.keySet()) {
                    get.setHeader(key, headers.get(key));
                }
            }

            response = client.execute(get);
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return resultString;
    }

    public static String doGet(String url) {
        return doGet(url, null, null);
    }

    public static String doGet(String url, Map<String, String> param) {
        return doGet(url, param, null);
    }

    /**
     * post方式调用httpclient服务
     *
     * @param url     请求地址
     * @param param   请求参数
     * @param headers 请求头信息
     * @return
     */
    public static String doPost(String url, Map<String, String> param, Map<String, String> headers) {

        String resultString = "";
        CloseableHttpResponse response = null;

        try {

            HttpPost httpPost = new HttpPost(url);
            if (param != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : param.keySet()) {
                    paramList.add(new BasicNameValuePair(key, param.get(key)));
                }
                // 模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, DEFAULT_CHARSET);
                httpPost.setEntity(entity);
            }

            if (headers != null) {
                for (String key : headers.keySet()) {
                    httpPost.setHeader(key, headers.get(key));
                }
            }

            response = client.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return resultString;

    }

    public static String doPost(String url, Map<String, String> param) {
        return doPost(url, param, null);
    }

    public static String doPost(String url) {
        return doPost(url, null, null);
    }

}
